package cn.actionListener;

import cn.action.AttackAction;
import cn.entity.master.Fighter;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class AttackListener implements KeyListener {
    private Fighter fighter;
    private AttackAction attackAction;
    public static Set<Integer> pressedKeys = new HashSet<>();

    public AttackListener(Fighter fighter){
        this.fighter = fighter;
        attackAction = new AttackAction(fighter);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        pressedKeys.add(key);

        try {
            attackAction.attackStart(key, pressedKeys, fighter);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();


        try {

            if (key == KeyEvent.VK_L) {
                attackAction.attackStop(key);

            } else {
                attackAction.attackStop(key);
            }
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        pressedKeys.remove(key);


    }
}